const logger = require('../logger');
const db = require('../../db');

class Rule {
    /*
    constructor() {

    }
    */


    /**
     * 规则列表
     */
    getRuleList(req) {
        let config_id = '';
        let condition = {};
        if (req.params) {
            config_id = req.params.config_id;
            if (config_id) {
                condition = { config_id };
            }
        }
        logger.data.info('model.rule.getRuleList req：', req.params);
        return new Promise(function (resolve, reject) {
            db.t_crawler_rule.findAndCountAll({
                // attributes: [],
                where: condition,
                order: [
                    ['update_time', 'desc']
                ],
            }).then(function (result) {
                resolve(result);
            }, function (err) {
                logger.error.error('model.rule.getRuleList error：', err);
                reject(err);
            });
        });
    }

    /**
     * 保存规则
     */
    saveRuleInfo(req) {
        let reqData = req.body;
        let { config_id } = reqData;
        const now = new Date();
        const uid = req.userInfo.uid;
        // logger.data.info('model.rule.saveRuleInfo req：', reqData);
        return new Promise(function (resolve, reject) {
            reqData.updator = uid;
            reqData.update_time = now;
            // 下面这些字段不必填，给默认值
            reqData.refresh_days = 0;
            reqData.refresh_count = 0;
            reqData.interval = 0;
            reqData.page_num_limit = 0;
            // 修改
            if (Number(config_id)) {
                logger.data.info('model.rule.saveRuleInfo update：', reqData);
                db.t_crawler_rule.update(reqData, {
                    where: { config_id }
                }).then(result => {
                    resolve(result[0]);
                }).catch(e => {
                    logger.error.error('model.rule.saveRuleInfo update error：', e);
                    resolve('');
                });
            } else { // 新增
                reqData.creator = uid;
                reqData.create_time = now;
                logger.data.info('model.rule.saveRuleInfo create：', reqData);
                db.t_crawler_rule.create(reqData).then(result => {
                    resolve(result);
                }).catch(e => {
                    logger.error.error('model.rule.saveRuleInfo create error：', e);
                    resolve('');
                });
            }
        });
    }

    /**
     * 删除规则（物理删除）
     */
    deleteRuleInfo(req) {
        let { config_id } = req.params;
        const uid = req.userInfo.uid;
        // logger.data.info('model.rule.saveRuleInfo req：', reqData);
        return new Promise(function (resolve, reject) {
            if (Number(config_id)) {
                logger.data.info('model.rule.deleteRuleInfo：', uid, config_id);
                db.t_crawler_rule.destroy({
                    where: { config_id }
                }).then(result => {
                    resolve(result);
                }).catch(e => {
                    logger.error.error('model.rule.deleteRuleInfo：', e);
                    resolve('');
                });
            } else {
                logger.error.error('model.rule.deleteRuleInfo：config_id is null');
            }
        });
    }
}


module.exports = new Rule();
